package com.ruoyi.park.controller;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.PreAuthorize;
import com.ruoyi.omp.api.domain.BuiRoom;
import com.ruoyi.park.domain.vo.BuiRoomVO;
import com.ruoyi.park.domain.vo.RoomFacilityExcelVO;
import com.ruoyi.park.service.IBuiRoomService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;

/**
 * 房源Controller
 *
 * @author cjie
 * @date 2021-07-20
 */
@Api(tags = " 房源")
@RestController
@RequestMapping("/buiRoom")
public class BuiRoomController extends BaseController {
    @Autowired
    private IBuiRoomService buiRoomService;

    /**
     * 查询房源列表(内部调用)
     */
    @PostMapping("/listRoom")
    public R<List<BuiRoom>> listRoom(@RequestBody BuiRoom buiRoom) {
        return R.ok(buiRoomService.selectBuiRoomList(buiRoom));
    }

    /**
     * 查询房源列表
     */
    @ApiOperation(value="查询房源列表", notes="查询房源列表")
    @PreAuthorize(hasPermi = "park:buiRoom:list")
    @GetMapping("/list")
    public TableDataInfo list(BuiRoom buiRoom) {
        startPage();
        List<BuiRoom> list = buiRoomService.selectBuiRoomList(buiRoom);
        return getDataTable(list);
    }

    /**
     * 导出房源列表
     */
    @ApiOperation(value="导出房源列表", notes="导出房源列表")
    @PreAuthorize(hasPermi = "park:buiRoom:export")
    @Log(title = "房源", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BuiRoom buiRoom) throws IOException {
        List<BuiRoom> list = buiRoomService.selectBuiRoomList(buiRoom);
        ExcelUtil<BuiRoom> util = new ExcelUtil<BuiRoom>(BuiRoom.class);
        util.exportExcel(response, list, "房源数据");
    }

    /**
     * 获取房源详细信息(内部调用)
     */
    @GetMapping(value = "/info/{roomId}")
    public R<BuiRoom> getRoom(@PathVariable("roomId") String roomId) {
        return R.ok(buiRoomService.selectBuiRoomById(roomId));
    }


    /**
     * 获取房源详细信息(内部调用)
     */
    @GetMapping(value = "/roomInfo/{roomNum}")
    public R<BuiRoom> getBuiRoomByNum(@PathVariable("roomNum") String roomNum) {

        BuiRoom query = new BuiRoom();
        query.setRoomNum(roomNum);
       List<BuiRoom> list =  buiRoomService.selectBuiRoomList(query);
       if (list.size() > 0){
           return R.ok(list.get(0));
       }else {
           return R.fail();
       }
    }



    @ApiOperation(value="获取房源详细信息", notes="获取房源详细信息")
    @PreAuthorize(hasPermi = "park:buiRoom:query")
    @GetMapping(value = "/{id}")
    public R<BuiRoom> getInfo(@PathVariable("id") String id) {
        return R.ok(buiRoomService.selectBuiRoomById(id));
    }

    /**
     * 新增房源
     */
    @ApiOperation(value="新增房源", notes="新增房源")
    @PreAuthorize(hasPermi = "park:buiRoom:add")
    @Log(title = "房源", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BuiRoom buiRoom) {
        return toAjax(buiRoomService.insertBuiRoom(buiRoom));
    }

    /**
     * 修改房源(内部调用)
     * @return
     */
    @Log(title = "修改房源", businessType = BusinessType.UPDATE)
    @PostMapping("/updateRoom")
    public R updateRoom(@RequestBody BuiRoom buiRoom) {
        return toR(buiRoomService.updateRoom(buiRoom));
    }
    /**
     * 修改房源
     */
    @ApiOperation(value="修改房源", notes="修改房源")
    @PreAuthorize(hasPermi = "park:buiRoom:edit")
    @Log(title = "房源", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BuiRoom buiRoom) {
        return buiRoomService.updateBuiRoom(buiRoom);
    }

    /**
     * 删除房源
     */
    @ApiOperation(value="删除房源", notes="删除房源")
    @ApiImplicitParam(name = "ids", value = "房源ID数组", required = true,paramType = "path", allowMultiple=true, dataType = "String")
    @PreAuthorize(hasPermi = "park:buiRoom:remove")
    @Log(title = "房源", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(buiRoomService.deleteBuiRoomByIds(ids));
    }

    /**
     * 通过租赁记录id查询租赁总面积(后台调用)
     * @param recordId 租赁记录id
     * @return
     */
    @GetMapping(value = "/sumArea/{recordId}")
    public R<BigDecimal> sumRoomAreaByRecordId(@PathVariable("recordId") String recordId){
        return R.ok(buiRoomService.sumAreaByRecordId(recordId));
    }

    /**
     * 通过租赁记录id查询房屋数量(后台调用)
     * @param recordId 租赁记录id
     * @return
     */
    @GetMapping(value = "/countRoom/{recordId}")
    public R<Integer> countRoomAreaByRecordId(@PathVariable("recordId") String recordId){
        return R.ok(buiRoomService.countRoomByRecordId(recordId));
    }

    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) throws IOException {
        ExcelUtil<BuiRoomVO> util = new ExcelUtil<BuiRoomVO>(BuiRoomVO.class);
        util.importTemplateExcel(response, "房源数据");
    }

    /**
     * 导入抄表记录列表,
     */
    @ApiOperation(value = "导入房源数据", notes = "导入房源数据")
    @PreAuthorize(hasPermi = "park:buiRoom:edit")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {

        ExcelUtil<BuiRoomVO> util = new ExcelUtil<BuiRoomVO>(BuiRoomVO.class);
        List<BuiRoomVO> buiRoomVOList = util.importExcel(file.getInputStream());
        //批量插入数据
        String message = buiRoomService.importData(buiRoomVOList);
        return AjaxResult.success(message);

    }


    @PostMapping("/importFacilityTemplate")
    public void importFacilityTemplate(HttpServletResponse response) throws IOException {
        ExcelUtil<RoomFacilityExcelVO> util = new ExcelUtil<RoomFacilityExcelVO>(RoomFacilityExcelVO.class);
        util.importTemplateExcel(response, "配套设施数据");
    }



    /**
     * 导入抄表记录列表,
     */
    @ApiOperation(value = "导入房源配套", notes = "导入房源配套")
    @PreAuthorize(hasPermi = "park:buiRoom:edit")
    @PostMapping("/importFacilityData")
    public AjaxResult importFacilityData(MultipartFile file, boolean updateSupport) throws Exception {

        ExcelUtil<RoomFacilityExcelVO> util = new ExcelUtil<>(RoomFacilityExcelVO.class);
        List<RoomFacilityExcelVO> buiRoomVOList = util.importExcel(file.getInputStream());
        //批量插入数据
        String message = buiRoomService.importFacilityData(buiRoomVOList);
        return AjaxResult.success(message);

    }



}
